package com.dragon.logical.service.avttendance.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.model.ResultCode;
import com.dragon.common.model.exception.CustomException;
import com.dragon.logical.service.avttendance.ITaskService;
import com.dragon.repository.entity.Task;
import com.dragon.repository.mapper.TaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-07-14
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements ITaskService {

    @Autowired
    TaskMapper taskMapper;

    @Override
    public Page<Task> taskList(Integer page, Integer limit, String search) {
        Page<Task> taskPage = new Page<>(page, limit);
        QueryWrapper<Task> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(StrUtil.isNotBlank(search),Task::getTaskName,search).or()
                        .like(StrUtil.isNotBlank(search),Task::getContent,search);
        Page<Task> selectPage = taskMapper.selectPage(taskPage, wrapper);
        return selectPage;
    }

    @Override
    public void addTask(Task task){
        task.setCreateTime(DateUtil.date());
        taskMapper.insert(task);
    }

    @Override
    public void updateTask(Task task){
        taskMapper.updateById(task);

    }

    @Override
    public void deleteTask(Integer id){
        Task task = taskMapper.selectById(id);
        if (task == null){
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }
        task.setIsDelete(1);
        taskMapper.updateById(task);

    }
}
